package com.zfei.gfb.service;

import com.google.common.collect.Lists;
import com.zfei.gfb.api.InboxService;
import com.zfei.gfb.api.domain.InboxTuple;
import com.zfei.gfb.api.domain.KeyTuple;
import com.zfei.gfb.api.domain.Page;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.device.InboxCache;
import com.zfei.gfb.util.Now;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;
import java.util.Set;


@Component
public class InboxServiceImpl implements InboxService {

    @Resource
    private InboxCache inboxCache;


    @Override
    public int insert(InboxTuple tuple) {
        this.inboxCache.insert(tuple, Duration.ofDays(365));
        return 0;
    }

    @Override
    public int insert(KeyTuple tuple) throws StatusException {
        this.inboxCache.insert(tuple, Duration.ofDays(30));
        return 0;
    }

    @Override
    public int delete(InboxTuple tuple) throws StatusException {
        this.inboxCache.delete(tuple);
        return 0;
    }

    @Override
    public int insert(List<InboxTuple> tuples) throws StatusException {
        return 0;
    }

    @Override
    public int delete(List<InboxTuple> tuples) throws StatusException {
        return 0;
    }

    @Override
    public int clear(String name) throws StatusException {
        return 0;
    }

    @Override
    public long count(String name, Long minTime, Long maxTime) throws StatusException {
        return this.inboxCache.count(name, minTime, maxTime);
    }

    @Override
    public long countOnly(String name, Long minTime, Long maxTime) throws StatusException {
        return 0;
    }

    @Override
    public KeyTuple get(String name, String eventId) throws StatusException {
        return this.inboxCache.get(name, eventId);
    }

    @Override
    public List<InboxTuple> get(String name, List<String> eventIds) throws StatusException {
        return null;
    }

    @Override
    public List<KeyTuple> list(String name, Page page) throws StatusException {
        if (page == null) {
            page = new Page();
        }
        page.build(Now::millis);
        if (!this.inboxCache.exists(name)) {
            return Lists.newArrayList();
        }
        return this.inboxCache.list(name, page.getMinTime(), page.getMaxTime(), page.getOffset(), page.getCount(),
                page.getOrderType());
    }

    @Override
    public List<InboxTuple> list(Set<String> names, Page page) throws StatusException {
        return null;
    }
}
